<?php

namespace App\Admin\Controllers;

use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Layout\Column;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Layout\Row;
use Illuminate\Support\Facades\Storage;


class FileManagerController extends AdminController
{

    public function index(Content $content)
    {
        return $content
            ->header('文件管理')
            ->body(function (Row $row) {
                $row->column(12, function (Column $column) {
                    $directories = $this->getDirectories();
                    $dir_path = request()->input('dir_path','public');
                    $dir_key = request()->input('dir_key','0-0');
                    $dirs = [ $directories ];
                    $props = [
                        'dirs'=>$dirs,
                        'dir_path'=>$dir_path,
                        'dir_key'=>$dir_key,
                    ];
                    $column->row(vue_render('FileManager/Index',$props));
                });
            });
    }

    /**
     * 获取目录信息
     */
    protected function getDirectories($dir = 'public',$title = 'public',$z_index = "0-0"): array
    {
        $dirs = Storage::disk('local')->directories($dir);
        $paths = [];
        if ($dirs){
            foreach ($dirs as $key=>$value){
                $zIndex = $z_index."-".$key;
                $paths[] = $this->getDirectories($value,str_replace($dir."/","",$value),$zIndex);
            }
        }
        return ['id'=>$z_index,'type'=>'menu', 'path'=>$dir, 'title'=> $title, 'children'=> $paths];
    }
}
